package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.*;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.MsgBiz;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * 多对多 项目——摄像头
 *
 * @author
 * @since
 */
@RestController
@RequestMapping("/admin/sendMessage")
public class SendMessageController extends BaseController<SendMessage> {

	@Autowired
	private SendMessageBiz sendMessageBiz;
	@Autowired
	private ActivityMapBiz activityMapBiz;
	@Autowired
	private EventBiz eventBiz;
	@Autowired
	private ManyBiz manyBiz;
	@Autowired
	private EquipmentBiz equipmentBiz;
	@Autowired
	private  CamerastatusBiz camerastatusBiz;
	@Autowired
	private  CameranotiyBiz cameranotiyBiz;
	@Autowired
	private  MsgBiz msgBiz;

	/*
	 *
	 * 页面列表显示
	 */
//	@PostMapping(value="/findPage")
//	@ResponseBody
//	public HttpResult findPage(@RequestBody SendMessage sendMessage) {
//		List<JSONObject> list = new ArrayList<JSONObject>();
//		JSONObject object = new JSONObject();
//		PageRequest pageRequest = new PageRequest();
//		PageResult result = null;
//		if(!"".equals(sendMessage.getPageNum())&&!"".equals(sendMessage.getPageSize())) {
//			pageRequest.setPageNum(sendMessage.getPageNum());
//			pageRequest.setPageSize(sendMessage.getPageSize());
//		}
//		//有项目展会名显示该展会规则内容
//		if(!"".equals(sendMessage.getItem())&&null!=sendMessage.getItem()) {
//			SendMessageExample sendMessageExample = new SendMessageExample();
//			sendMessageExample.setOrderByClause("id desc");
//			SendMessageExample.Criteria sendMessageExampleCriteria = sendMessageExample.createCriteria();
//			sendMessageExampleCriteria.andItemEqualTo(sendMessage.getItem());
//			sendMessageExampleCriteria.andStateEqualTo("0");
//			result=sendMessageBiz.findPage(pageRequest,sendMessageExample);
//
//		}
//		return HttpResult.ok(result);
//	}


	/**
	 * 添加发送信息人员
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addPerson", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> addRules(@RequestBody SendMessage sendMessage) throws Exception {
		String code = "500";
		String msg = "失败";
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();

		String item = sendMessage.getItem();// 项目名称
		String name = sendMessage.getName();
		String tel = sendMessage.getTel();

		String validity = "0";//默认有效
		String state = "0";//默认正常
		Date createTime = new Date();


		int no = 0;
		//设置状态默认为0 正常
		SendMessage message = new SendMessage();
		message.setItem(item);
		message.setName(name);
		message.setTel(tel);
		message.setValidity(validity);
		message.setCreatetime(createTime);
		message.setState(state);
		no = sendMessageBiz.insert(message);
		if (1 == no) {
			code = "200";
			msg = "成功";
		}
		map.put("code", code);
		map.put("msg", msg);
		return map;
	}

	/**
	 * 修改
	 * @param
	 * @return
	 */
	@PostMapping(value="/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody SendMessage sendMessage) {
		Map<String, Object> map = new HashMap<String, Object>();
		int no = 0;
		String code = "500";
		String msg = "失败";
		int id = sendMessage.getId();
		String item = sendMessage.getItem();// 项目名称
		String name = sendMessage.getName();//核验内容名称
		String tel = sendMessage.getTel();//活动名称

		SendMessageExample sendMessageExample = new SendMessageExample();
		SendMessageExample.Criteria sendMessageExampleCriteria = sendMessageExample.createCriteria();
		sendMessageExampleCriteria.andIdEqualTo(id);
		sendMessageExampleCriteria.andStateEqualTo("0");//状态正常数据
		List<SendMessage> sendMessages = sendMessageBiz.selectByExample(sendMessageExample);

		Date createTime = new Date();
		if(null!=sendMessages&&sendMessages.size()>0) {
			sendMessage.setCreatetime(createTime);
			no = sendMessageBiz.updateByPrimaryKeySelective(sendMessage);
		}else {
			msg = "联系人不存在";
		}
		if(1==no) {

			code = "200";
			msg = "成功";

		}
		map.put("code", code);
		map.put("msg",msg);
		return map;
	}

	/**
	 * 删除
	 * @param
	 * @return
	 */
	@PostMapping(value="/del")
	@ResponseBody
	public Map<String, Object> del(@RequestBody SendMessage sendMessage) {
		Map<String, Object> map = new HashMap<String, Object>();
		int no = 0;
		String code = "500";
		String msg = "失败";
		int id = sendMessage.getId();

		SendMessageExample sendMessageExample = new SendMessageExample();
		SendMessageExample.Criteria sendMessageExampleCriteria = sendMessageExample.createCriteria();
		sendMessageExampleCriteria.andIdEqualTo(id);
		sendMessageExampleCriteria.andStateEqualTo("0");//状态正常数据
		List<SendMessage> sendMessages = sendMessageBiz.selectByExample(sendMessageExample);


		Date createTime = new Date();
		if(null!=sendMessages&&sendMessages.size()>0) {
			sendMessage.setCreatetime(createTime);
			sendMessage.setState("1");
			no = sendMessageBiz.updateByPrimaryKeySelective(sendMessage);
		}else {
			msg = "规则不存在";
		}
		if(1==no) {
			code = "200";
			msg = "成功";

		}
		map.put("code", code);
		map.put("msg",msg);
		return map;
	}

	//更改启动线程参数数据
	@PostMapping(value = "/send")
	@ResponseBody
	public Map<String, Object> send(@RequestBody SendMessage sendMessage) {
		Map<String, Object> map = new HashMap<String, Object>();
		String code = "200";
		String msg = "成功";
		String name = sendMessage.getItem();
		String interval = sendMessage.getExstr1();
		SendMessageRunner.setName(name);
		SendMessageRunner.setInterval(interval);
		map.put("code", code);
		map.put("msg",msg);
		return map;
	}

}
